<?php
/*
 * Created on Sep 7, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<script type="text/javascript" src="<?=public_base_url()?>js/custom-form-elements.js"></script>
<?php
	define("NEWSLETTER_SEARCH", "NEWSLETTER_SEARCH");
	// Using model to get more information
	$CI =& get_instance();
	$CI->load->model('Model_newsgroup', 'model_newsgroup');
	$CI->load->model('Model_news', 'model_news');
	$CI->load->library('pagination');
	//$CI->load->library('htmlcutstring');
	$CI->load->helper('date');

	$gid = isset($_GET['gid']) && intval($_GET['gid']) ? intval($_GET['gid']) : intval($content['form']);
	$groups = $CI->model_newsgroup->fetch_all();
	// Pagination
	$start = 0;
	$limit = 10;
	$search = null;
	$keyword = null;
	$fromdate = date('Y-m-') . '01';
	$todate = date('Y-m-') . days_in_month(date('m'), date('Y'));
	$archive = null;

	// if it dont post
	if($CI->input->post('search')){
		$keyword = $CI->input->post('keyword');
		$archive = $CI->input->post('archive');
		$limit = $CI->input->post('limit');
		if(!intval($limit)){
			$limit = 10;
		}
		$fromdate = $archive.'-01';
		$to = $archive."-31";
		// add to session
		$search = array('keyword' => $keyword,
						'archive' => $archive,
						'limit' => $limit);
		$_SESSION[NEWSLETTER_SEARCH] = $search;
	}
	else{
		if(session_is_registered(NEWSLETTER_SEARCH)){
			$search = $_SESSION[NEWSLETTER_SEARCH];
			$keyword = $search['keyword'];
			$archive = $search['archive'];
			$limit = $search['limit'];
			$fromdate = $archive.'-01';
			$to = $archive."-31";
		}
		else{
			$search = array('keyword' => $keyword,
						'archive' => $archive,
						'limit' => $limit);
		}
	}

	$total = $CI->model_news->browse_count($keyword, null, $fromdate, $todate);
	$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
	if($per_page){
		$start = $per_page;
	}
	$newsletters = $CI->model_news->browse($keyword, null, $fromdate, $todate, $limit, $start);

	$config['base_url'] = $linkurl;
	$config['uri_segment'] = 5;
	$config['total_rows'] = $total;
	$config['per_page'] = $limit;

	$CI->pagination->initialize($config);
	//$CI->htmlcutstring->initialize(array('limit' => '20'));
?>
<form action="<?=htmlentities($linkurl.'&m=index')?>" method="post" id="newsLetterForm">
	<ul>
		<li><label>Keyword</label><input type="text" name="keyword" value="<?=$search['keyword']?>" class="input-text" maxlength="32" style="width: 110px; border-right: none;"/><input type="submit" name="search" class="input-go" value="Go" style=""/></li>
		<li><label>Archives</label>
			<select name="archive" class="styled" style="width: 132px" size="1">
				<option value="">Select All</option>
				<?php foreach (range(date('Y'), 2006, -1) as $year):?>
				<?php $month = 12; if(date('Y')==$year) { $month = date('m');}?>
				<?php foreach (range($month, 1, -1) as $item):?>
				<option value="<?=date("Y-m", mktime(0, 0, 0, $item, 1, $year))?>" <?= $search['archive'] == date("Y-m", mktime(0, 0, 0, $item, 1, $year)) ? "selected='true'" : ""?>>
					<?=date("F-Y", mktime(0, 0, 0, $item, 1, $year))?>
				</option>
				<?php endforeach;?>
				<?php endforeach;?>
			</select>
		</li>
	</ul>
</form>
<div class="news scrollfull">
	<div class="head">
		<div class="archive">NEWSLETTER <?=date("M-Y", strtotime($archive."-01"))?></div> <span>In this month's issue:</span>
	</div>
	<div class="addon">
		<p>Title: </p>
	</div>
	<div style="background-color: #b19e76;">
	<?php if(is_array($newsletters)): $i = 0;?>
		<div class="holder toi" style="width: 550px; background-image: none;">
			<div class="scroll-panel" style="height: 368px;">
			<?php foreach($newsletters as $item): $i += 1;?>
			<div class="item <?=$i%2==0 ? 'odd' : 'even'?>">
				<div class="img">
					<img src="<?=userfile_base_url().'image/'.$item['img']?>" style="max-width: 156px" title="<?=$item['caption']?>"/>
				</div>
				<div class="title"><?=$item['nam']?></div>
				<div class="desc">
					<?php if(empty($item['brief'])):?>
						<?= substr(strip_tags($item['description']), 0, 250) . "..."?>
					<?php else:?>
						<?= substr(strip_tags($item['brief']), 0, 250)?>
					<?php endif;?>
				</div>
				<div class="link">
					<a target="_self" href="<?=$linkurl.'&m=description&cid=' . $content['id'] . '&newsid='.$item['id']?>">Read more</a>
				</div>
				<div class="clearfix"></div>
			</div>
			<?php endforeach;?>
			</div>
		</div>
		<div class="pagination">
			<span style="font-weight: bold; color: #4C2C1D;">Results search: <?=$CI->pagination->total_rows?> items</span>
			<?= $CI->pagination->create_links();?>
		</div>
		<?php else:?>
		<div class="scrollfull">
			No results matching your search were found.
		</div>
	<?php endif;?>
	</div>
	<div class="clearfix"></div>
</div>
